package evidences

import (
  "errors"
  "go.mongodb.org/mongo-driver/bson"
  "go.mongodb.org/mongo-driver/mongo"
  "livefind/model/evidences"
  eviReq "livefind/service/evidences/request"
  eviRsp "livefind/service/evidences/response"
  "livefind/utils"
  "livefind/utils/logger"
  "mime/multipart"
)

type EvidenceService struct {
}

//@author: [Srim_ycb]
//@function: Create
//@description: 新建证据
//@param: req request.EvidenceCreateReq
//@return: err error
func (s *EvidenceService) Create(req eviReq.EvidenceCreateReq) (err error) {
  info := evidences.EvidenceInfo{}
  evidencesQuery, err := req.ConvertCreate2Query()
  if err != nil {
    return errors.New("案件ID错误")
  }
  filter := bson.M{
    "caseId": evidencesQuery.CaseID,
    "$or": bson.A{
      bson.M{
        "evidenceNo": evidencesQuery.EvidenceNo,
      },
      bson.M{
        "evidenceName": evidencesQuery.EvidenceName,
      },
    },
  }
  _, err = evidenceModel.FindEvidenceByFilter(filter)
  if err != nil {
    if err != mongo.ErrNoDocuments {
      logger.Error(err.Error(), err)
      return errors.New("创建证据失败")
    }
  } else {
    return errors.New("该证据号或证据名已存在")
  }

  utils.StructAssign(&info, evidencesQuery)

  err = evidenceModel.Create(info)

  if err != nil {
    return errors.New("创建案件失败")
  }
  return err
}

//@author: [Srim_ycb]
//@function: List
//@description: 获取证据列表
//@param: query request.EvidenceListReq
//@return: rsp {object} response.EvidenceListRsp, err error
func (s *EvidenceService) List(req eviReq.EvidenceListReq) (rsp eviRsp.EvidenceListRsp, err error) {
  evidenceQuery, err := req.ConvertList2Query()
  if err != nil {
    return rsp, errors.New("案件ID错误")
  }
  filter := bson.M{
    "caseId": evidenceQuery.CaseID,
  }
  evidencesInfo, err := evidenceModel.List(filter, evidenceQuery.Skip, evidenceQuery.Limit)

  if err != nil {
    return rsp, errors.New("获取证据列表失败")
  }
  totalNum, err := evidenceModel.CountDocuments(filter)
  if err != nil {
    return rsp, errors.New("获取证据列表失败")
  }

  rsp = eviRsp.ConvertList2Rsp(evidencesInfo, req.Page, req.PageSize, totalNum)
  return rsp, err
}

//@author: [Srim_ycb]
//@function: GetById
//@description: 根据id获取证据信息
//@param: query request.EvidenceGetReq
//@return: rsp {object} response.EvidenceGetRsp, err  error
func (s *EvidenceService) GetById(req eviReq.EvidenceGetReq) (rsp eviRsp.EvidenceGetRsp, err error) {
  mid, err := req.ConvertGetById2Query()
  if err != nil {
    return rsp, errors.New("证据ID错误")
  }
  filter := bson.M{
    "_id": mid,
  }

  evidenceInfo, err := evidenceModel.GetById(filter)

  if err != nil {
    return rsp, errors.New("获取案件信息失败")
  }

  rsp = eviRsp.ConvertGetById2Rsp(evidenceInfo)
  return rsp, err
}

func (s *EvidenceService) Upload(file *multipart.FileHeader) (rsp *eviRsp.UploadInfoRsp, err error) {
  path, fileName, err := fileUpload.UploadFile(file)
  if err != nil {
    logger.Error(err.Error(), err)
    return rsp, errors.New("文件写入本地失败")
  }
  rsp = eviRsp.ConvertUpload2Rsp(path, fileName)
  return rsp, nil
}
